<?php

namespace App\Livewire\Admin\Permission;

use Livewire\Attributes\Layout;
use Livewire\Component;
use Spatie\Permission\Models\Permission;

#[Layout('components.admin.layout')]
class Create extends Component
{
    public $name;
    public $description;
    public $group;

    public function store()
    {
        $validated = $this->validate([
            'name' => 'required',
            'description' => 'nullable',
            'group' => 'nullable',
        ]);
        $permission = new Permission();
        $permission->name = $this->name;
        $permission->description = $this->description;
        $permission->group = $this->group;
        $res = $permission->save();
        if ($res) {
            $html = view('admin.permission.list-item', ['permission' => $permission])->render();
            $this->dispatch('append-row', ['html' => $html]);
            $this->dispatch('close-modal');
        }
    }

    public function render()
    {
        return view('livewire.admin.permission.create');
    }
}
